package org.itboys.mobile.controller.site;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SundayAreaService;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.framework.spring.editor.ObjectIdEditor;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.entity.mongo.*;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mongo.site.*;
import org.itboys.mobile.service.mongo.web.SundayNewsCategoryService;
import org.itboys.mobile.service.mysql.site.SundaySiteProductService;
import org.itboys.mobile.service.mysql.web.product.SundayProductCategoryService;
import org.itboys.mobile.service.mysql.web.product.SundayProductService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/9/25
 * 描述：雄安加盟网_主页
 */
@Controller
@RequestMapping("/join/site/index")
public class JoinSiteIndexController extends BaseController{
    @Autowired
    private SundayProductCategoryService categoryService;
    @Autowired
    private SystemDictionaryService dictionaryService;
    @Autowired
    private SundaySiteProductService productService;
    @Autowired
    private SundayAreaService areaService;
    @Autowired
    private SundaySiteAdvertiseMentService advertiseMentService;
    @Autowired
    private SundaySiteMemberService memberService;
    @Autowired
    private SundayNewsCategoryService newsCategoryService;
    @Autowired
    private SundaySiteNewsService newsService;
    @Autowired
    private JoinSiteCountService countService;
    @Autowired
    private SundaySiteHangNewsService hangNewsService;

    /**
     * 首页
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String  index(HttpServletRequest request,
                         HttpServletResponse response, Model model){
        countService.count(model);
        //加载title  keywords  description
        List<SundayAdvertisementDto> index_titles =advertiseMentService.getAdvertisementByZone("index_title");
        model.addAttribute("index_titles",index_titles);
        List<SundayAdvertisementDto> index_descriptions =advertiseMentService.getAdvertisementByZone("index_description");
        model.addAttribute("index_descriptions",index_descriptions);
        List<SundayAdvertisementDto> index_keywords =advertiseMentService.getAdvertisementByZone("index_keywords");
        model.addAttribute("index_keywords",index_keywords);

        Map<String,Object> param=new HashMap<String,Object>();
        //加载二级行业分类
        param.clear();
        param.put("parentId !=",0);
        List<SundayProductCategory> secondCategories=categoryService.select(param);
        model.addAttribute("secondCategories",secondCategories);
        //加载轮播图1
        List<SundayAdvertisementDto> index_rolls =advertiseMentService.getAdvertisementByZone("index_roll");
        model.addAttribute("index_rolls",index_rolls);
        //加载轮播图2
        List<SundayAdvertisementDto> index_second_rolls =advertiseMentService.getAdvertisementByZone("index_roll2");
        model.addAttribute("index_second_rolls",index_second_rolls);
        //加载轮播图4
        List<SundayAdvertisementDto> index_four_rolls =advertiseMentService.getAdvertisementByZone("index_roll4");
        model.addAttribute("index_four_rolls",index_four_rolls);
        //加载首页轮播信息
        List<SundayAdvertisementDto> index_messages =advertiseMentService.getAdvertisementByZone("index_message");
        model.addAttribute("index_messages",index_messages);
        //加载资讯轮播图
        List<SundayAdvertisementDto> index_news_rolls =advertiseMentService.getAdvertisementByZone("index_news_roll");
        model.addAttribute("index_news_rolls",index_news_rolls);
        //加载首页插图1
        List<SundayAdvertisementDto> index_one_pictures =advertiseMentService.getAdvertisementByZone("index_roll3_1");
        model.addAttribute("index_one_pictures",index_one_pictures);
        //加载首页插图2
        List<SundayAdvertisementDto> index_two_pictures =advertiseMentService.getAdvertisementByZone("index_roll3_2");
        model.addAttribute("index_two_pictures",index_two_pictures);
        //加载首页插图3
        List<SundayAdvertisementDto> index_three_pictures =advertiseMentService.getAdvertisementByZone("index_roll3_3");
        model.addAttribute("index_three_pictures",index_three_pictures);
        //加载首页插图4
        List<SundayAdvertisementDto> index_four_pictures =advertiseMentService.getAdvertisementByZone("index_roll3_4");
        model.addAttribute("index_four_pictures",index_four_pictures);
        //加载首页三张展示图
        List<SundayAdvertisementDto> index_three_zhanshi_pictures =advertiseMentService.getAdvertisementByZone("index_roll5");
        model.addAttribute("index_three_zhanshi_pictures",index_three_zhanshi_pictures);
        //加载页面流程图
        List<SundayAdvertisementDto> index_process_pictures =advertiseMentService.getAdvertisementByZone("index_process");
        model.addAttribute("index_process_pictures",index_process_pictures);
        //加载首页需要的项目列表map集合
        Map<String,Object> productMap=productService.selectIndexProduct();
        model.addAttribute("productMap",productMap);
        //加载首页需要的品牌列表map集合
        Map<String,Object> memberMap=memberService.selectIndexMember();
        model.addAttribute("memberMap",memberMap);
        //加载首页上面三个需要的资讯列表
        List<SundayNewsCategory> newsRecommendCategories=newsCategoryService.getIndexRecommendNewsCategory();
        model.addAttribute("newsRecommendCategories",newsRecommendCategories);
        //加载首页下面四个需要的资讯列表
        List<SundayNewsCategory> newsCategories=newsCategoryService.getIndexNewsCategory();
        model.addAttribute("newsCategories",newsCategories);
        Map<String,Object> newss=newsService.getIndexNews();
        model.addAttribute("newss",newss);
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(8,null);
        model.addAttribute("paimingProducts",paimingProducts);
        //取所有分类下的最新6个
        List<SundayProduct> allNewSixProducts=productService.selectAllNewSix();
        model.addAttribute("allNewSixProducts",allNewSixProducts);

        model.addAttribute("allCategoryTitle",advertiseMentService.getCategoryAdvertisement());
        return "/join/site/index/index";
    }

    /**
     * 分类主页
     * @param request
     * @param response
     * @param parentId
     * @param secondId
     * @param model
     * @return
     */
    @RequestMapping("/second")
    public String  second(HttpServletRequest request,
                         HttpServletResponse response,
                          @RequestParam(value="parentId",required=false)Long parentId,
                          @RequestParam(value="secondId",required=false)Long secondId,
                          Model model){
        //加载页面通用
        countService.count(model);
        //投资金额
        List<SystemDictionary> amounts=dictionaryService.getDictionaryByKey("invest_amount");
        model.addAttribute("amounts",amounts);
        Map<String,Object> param=new HashMap<String,Object>();
        //父行业
        if(LongUtil.isNotZreo(parentId)){
            SundayProductCategory thisParentCategory=categoryService.find(parentId);
            model.addAttribute("thisParentCategory",thisParentCategory);
            param.clear();
            param.put("parentId",parentId);
            List<SundayProductCategory> secondCategories=categoryService.select(param);
            model.addAttribute("secondCategories",secondCategories);
            //获取子行业页面需要的品牌信息
            Map<String,Object> memberMap=memberService.selectSecondMember(parentId,null);
            model.addAttribute("secondMemberMap",memberMap);
            Map<String,Object> productMap=productService.selectSecondProduct(parentId,null);
            model.addAttribute("productMap",productMap);
            //加载排名项目
            List<SundayProduct>paimingProducts=productService.selectPaiming(10,parentId);
            model.addAttribute("paimingProducts",paimingProducts);
            //行业页面轮播图
            List<SundayAdvertisementDto> second_rolls =advertiseMentService.getAdvertisementByZone(parentId+"zoneKey");
            model.addAttribute("second_rolls",second_rolls);
            //行业页面插图
            List<SundayAdvertisementDto> second_insert_rolls =advertiseMentService.getAdvertisementByZone(parentId+"zoneKey_insert");
            model.addAttribute("second_insert_rolls",second_insert_rolls);
            //行业页面特约头条
            List<SundayAdvertisementDto> second_teyue_rolls =advertiseMentService.getAdvertisementByZone(parentId+"zoneKey_teyue");
            model.addAttribute("second_teyue_rolls",second_teyue_rolls);
            //行业页面网页title
            List<SundayAdvertisementDto> second_title_rolls =advertiseMentService.getAdvertisementByZone(parentId+"zoneKey_title");
            model.addAttribute("second_title_rolls",second_title_rolls);
            //行业页面网页关键词
            List<SundayAdvertisementDto> second_keywords_rolls =advertiseMentService.getAdvertisementByZone(parentId+"zoneKey_keywords");
            model.addAttribute("second_keywords_rolls",second_keywords_rolls);
            //行业页面网页描述
            List<SundayAdvertisementDto> second_description_rolls =advertiseMentService.getAdvertisementByZone(parentId+"zoneKey_description");
            model.addAttribute("second_description_rolls",second_description_rolls);
            //行业页面四张新闻图
            List<SundayAdvertisementDto> second_news_images =advertiseMentService.getAdvertisementByZone(parentId+"news_image");
            model.addAttribute("second_news_images",second_news_images);
            //行业页面新闻
            List<SundayHangNews> newss=hangNewsService.getSecondNews(parentId);
            model.addAttribute("newss",newss);
            //行业新闻类目
            List<SundayHangNewsCategory> newsCategories=hangNewsService.getSecondNewsCategory(parentId);
            model.addAttribute("newsCategories",newsCategories);

        }
        //子行业
        if(LongUtil.isNotZreo(secondId)){
            SundayProductCategory thisSecondCategory=categoryService.find(secondId);
            model.addAttribute("thisSecondCategory",thisSecondCategory);
            SundayProductCategory thisParentCategory=categoryService.find(thisSecondCategory.getParentId());
            model.addAttribute("thisParentCategory",thisParentCategory);
            param.clear();
            param.put("parentId",thisParentCategory.getId());
            List<SundayProductCategory> secondCategories=categoryService.select(param);
            model.addAttribute("secondCategories",secondCategories);
            Map<String,Object> productMap=productService.selectSecondProduct(null,secondId);
            model.addAttribute("productMap",productMap);
            //获取子行业页面需要的品牌信息
            Map<String,Object> memberMap=memberService.selectSecondMember(null,secondId);
            model.addAttribute("secondMemberMap",memberMap);

            //加载排名项目
            List<SundayProduct>paimingProducts=productService.selectPaiming(10,thisParentCategory.getId());
            model.addAttribute("paimingProducts",paimingProducts);
            //行业页面轮播图
            List<SundayAdvertisementDto> second_rolls =advertiseMentService.getAdvertisementByZone(secondId+"zoneKey");
            model.addAttribute("second_rolls",second_rolls);
            //行业页面插图
            List<SundayAdvertisementDto> second_insert_rolls =advertiseMentService.getAdvertisementByZone(secondId+"zoneKey_insert");
            model.addAttribute("second_insert_rolls",second_insert_rolls);
            //行业页面特约头条
            List<SundayAdvertisementDto> second_teyue_rolls =advertiseMentService.getAdvertisementByZone(secondId+"zoneKey_teyue");
            model.addAttribute("second_teyue_rolls",second_teyue_rolls);
            //行业页面网页title
            List<SundayAdvertisementDto> second_title_rolls =advertiseMentService.getAdvertisementByZone(secondId+"zoneKey_title");
            model.addAttribute("second_title_rolls",second_title_rolls);
            //行业页面网页关键词
            List<SundayAdvertisementDto> second_keywords_rolls =advertiseMentService.getAdvertisementByZone(secondId+"zoneKey_keywords");
            model.addAttribute("second_keywords_rolls",second_keywords_rolls);
            //行业页面网页描述
            List<SundayAdvertisementDto> second_description_rolls =advertiseMentService.getAdvertisementByZone(secondId+"zoneKey_description");
            model.addAttribute("second_description_rolls",second_description_rolls);
            //行业页面四张新闻图
            List<SundayAdvertisementDto> second_news_images =advertiseMentService.getAdvertisementByZone(secondId+"news_image");
            model.addAttribute("second_news_images",second_news_images);
            //行业页面新闻
            List<SundayHangNews> newss=hangNewsService.getSecondNews(secondId);
            model.addAttribute("newss",newss);
            //行业新闻类目
            List<SundayHangNewsCategory> newsCategories=hangNewsService.getSecondNewsCategory(secondId);
            model.addAttribute("newsCategories",newsCategories);
        }
        param.clear();
        param.put("parentId",0);
        param.put("sort_name","sequence");
        param.put("sort_order","asc");
        List<SundayProductCategory> categories= categoryService.select(param);
        model.addAttribute("categories",categories);
        //加载资讯轮播图
        List<SundayAdvertisementDto> index_news_rolls =advertiseMentService.getAdvertisementByZone("index_news_roll");
        model.addAttribute("index_news_rolls",index_news_rolls);
        //加载首页需要的品牌列表map集合
        Map<String,Object> memberMap=memberService.selectIndexMember();
        model.addAttribute("memberMap",memberMap);
        //加载页面流程图
        List<SundayAdvertisementDto> index_process_pictures =advertiseMentService.getAdvertisementByZone("index_process");
        model.addAttribute("index_process_pictures",index_process_pictures);
        return "/join/site/index/second";
    }
    @RequestMapping("/search")
    public String  search(HttpServletRequest request,
                          HttpServletResponse response,
                          @RequestParam(value="parentId",required=false)Long parentId,
                          @RequestParam(value="secondId",required=false)Long secondId,
                         @RequestParam(value="investAmount",required=false)String investAmount,
                         @RequestParam(value="brandSource",required=false)String brandSource,
                          @RequestParam(value="name",required=false)String name,
                          @RequestParam(value="page",required=false)Integer page,
                          Model model){
        //加载一些广告
        countService.count(model);
        Map<String,Object> param=new HashMap<String,Object>();
        if(LongUtil.isNotZreo(parentId)){
            SundayProductCategory thisParentCategory=categoryService.find(parentId);
            model.addAttribute("thisParentCategory",thisParentCategory);
            param.clear();
            param.put("parentId",parentId);
            List<SundayProductCategory> secondCategories=categoryService.select(param);
            model.addAttribute("secondCategories",secondCategories);

        }
        if(LongUtil.isNotZreo(secondId)){
            SundayProductCategory thisSecondCategory=categoryService.find(secondId);
            model.addAttribute("thisSecondCategory",thisSecondCategory);
            SundayProductCategory thisParentCategory=categoryService.find(thisSecondCategory.getParentId());
            model.addAttribute("thisParentCategory",thisParentCategory);
            param.clear();
            param.put("parentId",thisSecondCategory.getParentId());
            List<SundayProductCategory> secondCategories=categoryService.select(param);
            model.addAttribute("secondCategories",secondCategories);
            }
        List<SystemDictionary> amounts=dictionaryService.getDictionaryByKey("invest_amount");
        model.addAttribute("amounts",amounts);
        if(StringUtils.isNotBlank(investAmount)){
            model.addAttribute("investAmount",investAmount);
        }
        if(StringUtils.isNotBlank(brandSource)){
            model.addAttribute("brandSource",brandSource);
        }
        if(StringUtils.isNotBlank(name)){
            model.addAttribute("name",name);
        }
        model.addAttribute("provinces",areaService.getProvince());
        PageResult<SundayProduct> result=productService.searchProduct(parentId,secondId,investAmount,brandSource, name, page);
        Long totalPage=result.getTotal()/ SundayCommonConstants.row_size+1;
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage",page);
        model.addAttribute("products",result.getData());
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(10,null);
        model.addAttribute("paimingProducts",paimingProducts);
        //获取好项目
        List<SundayProduct> goodProducts=productService.selectGood(null);
        model.addAttribute("goodProducts",goodProducts);
        return "/join/site/index/search";
    }
}
